﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

namespace Tank.Draw
{
    public class Checkpoint
    {
        public void setCheckpoint(int i)
        {
            var data = Data.MapDatas.Where(p => p.level == i).FirstOrDefault();
            if (data == null)
                throw new Exception("当前级别的地图不存在");
            OneCheckpoint(data);
        }
        /// <summary>
        /// 第一关
        /// </summary>
        private void OneCheckpoint(MapData data)
        {
            var arr = data.data;
            Dictionary<Point, Data.mapImage> mapDic = new Dictionary<Point, Data.mapImage>();
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    switch (oneLevel[i, j])
                    {
                        //x,y轴反了，不知道为啥
                        case 1:
                            mapDic.Add(new Point(j * Data.GridSize, i * Data.GridSize), Data.mapImage.wall);
                            break;
                        case 2:
                            mapDic.Add(new Point(j * Data.GridSize, i * Data.GridSize), Data.mapImage.steel);
                            break;
                        case 3:
                            mapDic.Add(new Point(j * Data.GridSize, i * Data.GridSize), Data.mapImage.grass);
                            break;
                        case 4:
                            mapDic.Add(new Point(j * Data.GridSize, i * Data.GridSize), Data.mapImage.water);
                            break;
                    }
                }
            }
            //砖墙
            //addElement(getListPoint(new Point(2, 2), new Point(3, 10)), ref mapDic, Data.mapImage.wall);
            //addElement(getListPoint(new Point(6, 2), new Point(7, 10)), ref mapDic, Data.mapImage.wall);
            //addElement(getListPoint(new Point(10, 2), new Point(11, 8)), ref mapDic, Data.mapImage.wall);
            //addElement(getListPoint(new Point(14, 2), new Point(15, 8)), ref mapDic, Data.mapImage.wall);
            //addElement(getListPoint(new Point(18, 2), new Point(19, 10)), ref mapDic, Data.mapImage.wall);
            //addElement(getListPoint(new Point(22, 2), new Point(23, 10)), ref mapDic, Data.mapImage.wall);

            //addElement(getListPoint(new Point(10, 11), new Point(11, 12)), ref mapDic, Data.mapImage.wall);
            //addElement(getListPoint(new Point(14, 11), new Point(15, 12)), ref mapDic, Data.mapImage.wall);
            //addElement(getListPoint(new Point(4, 13), new Point(7, 14)), ref mapDic, Data.mapImage.wall);
            //addElement(getListPoint(new Point(18, 13), new Point(21, 14)), ref mapDic, Data.mapImage.wall);

            //addElement(getListPoint(new Point(2, 17), new Point(3, 23)), ref mapDic, Data.mapImage.wall);
            //addElement(getListPoint(new Point(6, 17), new Point(7, 20)), ref mapDic, Data.mapImage.wall);
            //addElement(getListPoint(new Point(10, 15), new Point(11, 20)), ref mapDic, Data.mapImage.wall);
            //addElement(getListPoint(new Point(12, 16), new Point(13, 17)), ref mapDic, Data.mapImage.wall);
            //addElement(getListPoint(new Point(14, 15), new Point(15, 20)), ref mapDic, Data.mapImage.wall);
            //addElement(getListPoint(new Point(18, 17), new Point(19, 23)), ref mapDic, Data.mapImage.wall);
            //addElement(getListPoint(new Point(22, 17), new Point(23, 23)), ref mapDic, Data.mapImage.wall);

            //addElement(getListPoint(new Point(11, 23), new Point(11, 25)), ref mapDic, Data.mapImage.wall);
            //addElement(getListPoint(new Point(14, 23), new Point(14, 25)), ref mapDic, Data.mapImage.wall);
            //mapDic.Add(new Point(12 * Data.GridSize, 23 * Data.GridSize), Data.mapImage.wall);
            //mapDic.Add(new Point(13 * Data.GridSize, 23 * Data.GridSize), Data.mapImage.wall);
            ////金刚墙
            //addElement(getListPoint(new Point(0, 14), new Point(1, 14)), ref mapDic, Data.mapImage.steel);
            //addElement(getListPoint(new Point(24, 14), new Point(25, 14)), ref mapDic, Data.mapImage.steel);
            //addElement(getListPoint(new Point(12, 6), new Point(13, 7)), ref mapDic, Data.mapImage.steel);

            Map.setMap(mapDic, data.level);
        }

        /// <summary>
        /// 获取两个坐标中所有坐标
        /// </summary>
        /// <param name="minPoint"></param>
        /// <param name="maxPoint"></param>
        /// <returns></returns>
        private List<Point> getListPoint(Point minPoint, Point maxPoint)
        {
            List<Point> p_list = new List<Point>();
            int x = maxPoint.X - minPoint.X;
            int y = maxPoint.Y - minPoint.Y;
            for (int i = 0; i <= x; i++)
            {
                for (int j = 0; j <= y; j++)
                {
                    Point p = new Point((minPoint.X + i) * Data.GridSize, (minPoint.Y + j) * Data.GridSize);
                    p_list.Add(p);
                }
            }
            return p_list;
        }


        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="p_list"></param>
        /// <param name="mapDic"></param>
        private void addElement(List<Point> p_list, ref Dictionary<Point, Data.mapImage> mapDic, Data.mapImage mapImage)
        {
            foreach (var item in p_list)
            {
                mapDic.Add(item, mapImage);
            }
        }

        /// <summary>
        /// 第一关 0,无 
        /// </summary>
        private int[,] oneLevel = new int[26, 26] {
            { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0},
            { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0},
            { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0},
            { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0},
            { 0,0,1,1,0,0,1,1,0,0,1,1,2,2,1,1,0,0,1,1,0,0,1,1,0,0},
            { 0,0,1,1,0,0,1,1,0,0,1,1,2,2,1,1,0,0,1,1,0,0,1,1,0,0},
            { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0},
            { 0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0},
            { 0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0},
            { 0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
            { 0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
            { 0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0},
            { 2,2,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,2,2},
            { 0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
            { 0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},
            { 0,0,1,1,0,0,1,1,0,0,1,1,1,1,1,1,0,0,1,1,0,0,1,1,0,0},
            { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0},
            { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0},
            { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0},
            { 0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0},
            { 0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0},
            { 0,0,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0,1,1,0,0,1,1,0,0},
            { 0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
            { 0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0}
        };


    }
}
